//
//  RegistViewController.swift
//  daily_count
//
//  Created by 段帅 on 2018/4/2.
//  Copyright © 2018年 Sooc. All rights reserved.
//

import UIKit

class RegistViewController: UIViewController {

    
    @IBOutlet weak var count_filed: UITextField!
    
    @IBOutlet weak var password_filed_one: UITextField!
    
    @IBOutlet weak var password_filed_two: UITextField!
    
    @IBOutlet weak var name_filed: UITextField!
    
    @IBOutlet weak var phone_filed: UITextField!
    
    @IBOutlet weak var age_filed: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func regist(_ sender: Any) {
        //首先验证各 输入框 是否为空， 然后验证 账号和密码是否大于 六位  ，最后验证 密码是否一致。
        if (count_filed.hasText&&password_filed_one.hasText&&password_filed_two.hasText&&name_filed.hasText&&phone_filed.hasText&&age_filed.hasText) {
            
            if checkCountAndPassword() {
                if password_filed_two.text == password_filed_one.text {
                    registCount()
                }else{
                     DSToolManager.alertFaild(message: "两次密码不一致") 
                }
            }else{
                DSToolManager.alertFaild(message: "账号和密码需要大于6位") 
            }
        }else{
            DSToolManager.alertFaild(message: "请输入完整资料")
        }
    }

    //取消注册
    @IBAction func cancleRegist(_ sender: Any) {
        self.dismiss(animated: true, completion: nil);
    }
    
    /// 注册用户
    func registCount() {
        var para = [String:AnyObject]();
        para["count"] = self.count_filed.text as AnyObject;
        para["password"] = self.password_filed_one.text as AnyObject;
        para["age"] = self.age_filed.text as AnyObject;
        para["phone"] = self.phone_filed.text as AnyObject;
        para["name"]  = self.name_filed.text as AnyObject;
        
        
        
        DSToolManager.post(URLString: severseUrl+api_regist, parameters: para) { (data, isSucess) in
            if isSucess {
                DSToolManager.alertSucess(message: "注册成功")
                DispatchQueue.main.asyncAfter(deadline: .now()+1.5, execute: { 
                    self.dismiss(animated: true, completion: nil);
                })
            }else{
                DSToolManager.alertFaild(message: data!["statusCode"] as! String);
            }
        }
        
        
    }
    func checkCountAndPassword() -> Bool {
        if ((count_filed.text?.count)! < 6 || (password_filed_one.text?.count)! < 6) {
            return false;
        }else{
            return true;
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true);
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
